﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.ArchiveMange.Models;
using System.Data;
using DreamBuilder.Core.Container;
using ePlanet.ArchiveMange.Services;
using ePlanet.ArchiveMange.Enum;
using Web.SystemConfig.Services;
using Microsoft.Practices.Unity;
using Wuqi.Webdiyer;
using Web.SystemConfig.WorkFlow;
using System.Collections;

namespace Web.Views.ArchiveManage
{
    public partial class BuildProjectArchiveList : ArchiveFlowPageBase
    {
        #region 属性
        /// <summary>
        /// 文档类型ID
        /// </summary>
        public int ArchiveTypeID
        {
            get
            {
                int o_int = 0;
                if (Request.QueryString["archivetype"] != null)
                {
                    if (int.TryParse(Request.QueryString["archivetype"], out o_int))
                    {
                        return o_int;
                    }
                    else
                    {
                        throw new Exception("档案类型错误");
                    }
                }
                else
                {
                    throw new Exception("档案类型不能为空");
                }
            }
        }

        #endregion

        #region 获取数据源


        /// <summary>
        /// 获取未移交的数据
        /// </summary>
        /// <returns></returns>
        private DataTable GetData()
        {
            int baseTypeId = IArchiveMenuService.GetArchiveBaseIDById(ArchiveTypeID);
            DataTable dt = null;
            switch (baseTypeId)
            {
                case((int)ArchiveBaseType.BulidProject):
                    IBuildProjectArchiveService buildproject = UContainer.Resolve<IBuildProjectArchiveService>();
                    dt = buildproject.GetDocumenArchivetDataByType(ArchiveTypeID.ToString());
                    break;
            }
            return dt;
        }

        /// <summary>
        /// 获取已经移交的数据
        /// </summary>
        /// <returns></returns>
        private DataTable GetDoData()
        {
            //int baseTypeId = ArchiveMenuService.GetArchiveBaseIDById(ArchiveTypeID);
            DataTable dt = null;
            //switch (baseTypeId)
            //{
            //    case ((int)ArchiveBaseType.BulidProject):
            //        IBuildProjectArchiveService buildproject = UContainer.Resolve<IBuildProjectArchiveService>();
            //        dt = buildproject.GetDocumenArchivetDataByType(ArchiveTypeID.ToString());
            //        break;
            //}
            return dt;
        }
        #endregion
        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            this.sch.SearchEvent+=new EventHandler(sch_SearchEvent);
            gvTransfered = this.gv_Archive_do;
            pagerTransfered = this.pagerdo;

            gvUnTransfered = this.gv_Archive;
            pagerUntransfered = this.pager;

            //是否本单位管理逻辑
            chkIsDepartmentReview = chkboxIsDepartmentReview;
            if (this.CurrentUser.Org_FullFondsNum == CommonUtil.GetConfigValue("ArchiveManage", "InstitutionFullFondsNum"))
                chkboxIsDepartmentReview.Visible = false;

            searchCtrl = sch;

            pagerTransfered.PageSizeChanging += new EventHandler(pagerTransfered_PageSizeChanging);
            pagerUntransfered.PageSizeChanging += new EventHandler(pagerUntransfered_PageSizeChanging);

            //CurrentArchiveKind = ArchiveTableEnum.BulidProject;
            PassFlowName = this.hiddenCurrentFlowName.Value;
            CurrentStatusName = this.hiddenStatusName.Value;

            //if (this.CurrentUser.Org_FullFondsNum == CommonUtil.GetConfigValue("ArchiveManage", "InstitutionFullFondsNum"))
            //{
            //    btnPrintHandoverCatagory.Visible = true;
            //}
            //else
            //    btnPrintHandoverCatagory.Visible = false;

            if (!IsPostBack)
            {
                this.archiveTypeId.Value = this.ArchiveTypeID.ToString();
                //BindGrid();
                //BindDoGrid();
                InitialData();
            }

            string webAddress = "http://" + this.Request.Url.Authority + this.Request.ApplicationPath;
            webAddress = HttpUtility.UrlEncode(webAddress);
            try
            {
                this.AddArchiveBatch.NavigateUrl = "~/FilesUploadClickOnce/FilesUpload.application?UserId=" + this.CurrentUser.User_ID + "&WebAddress=" + webAddress + "&CatalogId=" + this.ArchiveTypeID + "&Type=Archive";
            }
            catch (Exception)
            {
                this.AddArchiveBatch.Enabled = false;
            }
        }

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sch_SearchEvent(object sender, EventArgs e)
        {
            InitialData(this.sch.WhereString);
        }

        #region gv_Archive事件
        protected void gr_Archive_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //ArchiveMangeModal.BULIDPROJECT_ARCHIVERow row = ((DataRowView)e.Row.DataItem).Row as ArchiveMangeModal.BULIDPROJECT_ARCHIVERow;
                DataRowView row = ((DataRowView)e.Row.DataItem);
                string elecFileCount = ArchiveBusiHelper.Instance.GetTechnologyFileCount((int)ArchiveBaseType.BulidProject, row["dataid"].ToString());
                if (!string.IsNullOrEmpty(elecFileCount))
                {
                    e.Row.Cells[1].Text = string.Format("<img src='{0}' alt='{1}个电子文件' />",
                        "../../Images/qbz.jpg", elecFileCount);
                }
                else
                {
                    e.Row.Cells[1].Text = string.Empty;
                }
                e.Row.Cells[4].Text = IUserService.GetUserNameById(row["CREATEUSERID"].ToString());
                e.Row.Cells[12].Text = ICodeTableService.GetpkNameByCodepkValue(row["F_SECURITYTYPE"].ToString());
            }
        }

        protected void pager_PageChanging(object sender, PageChangingEventArgs e)
        {
            pager.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void gr_Archive_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string pkValue = this.gv_Archive.DataKeys[e.NewEditIndex].Value.ToString();
            this.Response.Redirect("ArchiveEdit.aspx?archiveId=" + pkValue + "&archivetype=" + ArchiveTypeID + "");
        }

        protected void gv_Archive_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //string pkValue = e.Values[""];e.
            try
            {
                string pkValue = this.gv_Archive.DataKeys[e.RowIndex].Value.ToString();
                IBuildProjectArchiveService document = UContainer.Resolve<IBuildProjectArchiveService>();
                bool bReturn = document.DeleteData(pkValue);
                if (bReturn)
                {
                    InitialData();
                    Alert("删除成功！");
                }
            }
            catch
            {
                Alert("操作出现异常，请重新再试！");
            }
        }
        #endregion
        #region gv_Archive_do事件
        protected void gr_Archive_do_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow row = ((DataRowView)e.Row.DataItem).Row ;
                string elecFileCount = ArchiveBusiHelper.Instance.GetTechnologyFileCount((int)ArchiveBaseType.BulidProject, row["dataid"].ToString());
                if (!string.IsNullOrEmpty(elecFileCount))
                {
                    e.Row.Cells[1].Text = string.Format("<img src='{0}' alt='{1}个电子文件' />",
                        "../../Images/qbz.jpg", elecFileCount);
                }
                else
                {
                    e.Row.Cells[1].Text = string.Empty;
                }
                e.Row.Cells[3].Text = IUserService.GetUserNameById(row["CREATEUSERID"].ToString());
                e.Row.Cells[7].Text = ICodeTableService.GetpkNameByCodepkValue(row["F_SECURITYTYPE"].ToString());
                e.Row.Cells[4].Text = string.Format("<a target='_blank' href='../ArchiveView/ArchivePreview.aspx?op=scan&archiveId={0}&archivetype={1}&currentstatusname={2}' >{3}</a>",
                    row["DATAID"], (int)CurrentArchiveKind, CurrentStatusName, row["TITLE"]);
            }
        }

        protected void pagerdo_PageChanging(object sender, PageChangingEventArgs e)
        {
            pagerdo.CurrentPageIndex = e.NewPageIndex;
            BindDoGrid();
        }
        #endregion
        #endregion
        #region  私有方法
        /// <summary>
        /// 绑定GridView
        /// </summary>
        private void BindGrid()
        {
            int recordCount = 0;
            DataTable dt = GetData();
            this.gv_Archive.DataSource = PageDataList.GetPagedTable(dt, pager.CurrentPageIndex, pager.PageSize, out recordCount);
            pager.RecordCount = recordCount;
            this.gv_Archive.DataBind();
        }

        /// <summary>
        /// 绑定GridView
        /// </summary>
        private void BindDoGrid()
        {
            int recordCount = 0;
            DataTable dt = GetDoData();
            this.gv_Archive_do.DataSource = PageDataList.GetPagedTable(dt, pagerdo.CurrentPageIndex, pagerdo.PageSize,out recordCount);
            pagerdo.RecordCount = recordCount;
            this.gv_Archive_do.DataBind();
        }
        #endregion

        #region 按钮事件
        protected void btnPrintArchiveMenu_Click(object sender, EventArgs e)
        {
            ArrayList keys = new ArrayList();
            keys.AddRange(this.gv_Archive.SelectItems);
            if (this.gv_Archive_do.SelectItems != null){ keys.AddRange(this.gv_Archive_do.SelectItems);}
            if (keys.Count == 0)
            {
                Alert("未选中任何数据！");
                return;
            }
            Session["selectedkeys"] = keys;
            this.Response.Redirect(string.Format("../ArchiveFlowView/PrintEntCatagory.aspx?from=dazl&archiveType={0}", (int)CurrentArchiveKind));
        }

        /// <summary>
        /// 备注：打印档案交接文具
        /// 时间：2013年3月12日 23:21:21
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrintHandoverCatagory_Click(object sender, EventArgs e)
        {
            ArrayList keys = new ArrayList();
            keys.AddRange(this.gv_Archive.SelectItems);
            if (this.gv_Archive_do.SelectItems != null){ keys.AddRange(this.gv_Archive_do.SelectItems);}
            if (keys.Count == 0)
            {
                Alert("未选中任何数据！");
                return;
            }
            Session["dayj_selectedkeys_jj"] = keys;
            //this.Response.Redirect(string.Format("../ArchiveFlowView/PrintHandoverCatagory.aspx?from=dayj&archiveType={0}", (int)CurrentArchiveKind));
            this.RegisterClientScriptBlock("alert",
                string.Format("<script>window.open('../ArchiveFlowView/AddHandoverDescView.aspx?from=dayj&archiveType={0}', '_blank', 'width=600,height=445,resizable=no');</script>"
                , (int)CurrentArchiveKind));
        }

        protected void bnPrintJuanCatagory_Click(object sender, EventArgs e)
        {
            ArrayList keys = new ArrayList();
            keys = this.gv_Archive.SelectItems;
            keys.AddRange(this.gv_Archive_do.SelectItems);
            if (keys.Count == 0)
            {
                Alert("未选中任何数据！");
                return;
            }
            Session["dasj_jnml_selectedkeys"] = keys;
            this.Response.Redirect(string.Format("../ArchiveFlowView/PrintJuanCatagoryView.aspx?from=dasj&archiveType={0}", (int)CurrentArchiveKind));
        }

        protected void btnPrintBeikaoTable_Click(object sender, EventArgs e)
        {
            ArrayList keys = new ArrayList();
            keys = this.gv_Archive.SelectItems;
            keys.AddRange(this.gv_Archive_do.SelectItems);
            if (keys.Count == 0)
            {
                Alert("未选中任何数据！");
                return;
            }

            Session["dasj_bkb_selectedkeys"] = keys;
            Response.Redirect("../ArchiveFlowView/PrintBeikaoTableView.aspx?from=dasj&archiveType=" + (int)BaseArchiveType);

        }

        #endregion
    }
}
